package WarTug;

public class GardenOptionsWindow {

	public final int WINDOW_WIDTH = 160;
	public final int WINDOW_HEIGHT = 160;
	public final int SCREENX = 340;
	public final int SCREENY = 440;
	private final int GRID_WIDTH = 4;
	private final int GRID_HEIGHT = 4;
	private final int CELL_WIDTH = 32;
	private Garden garden;
	private int[] selectedChoice = {0, 0};
	private DrawableImage selectedImage;
	private DrawableImage[][] images = new DrawableImage[GRID_HEIGHT][GRID_WIDTH];
	private DrawableImage[][] backImages = new DrawableImage[GRID_HEIGHT][GRID_WIDTH];
	// This is a comment about the line below
	public boolean unlockUnlocked = false;
	
	private GardenSpaceType[][] choices = 
			{{GardenSpaceType.unlock, GardenSpaceType.lion, GardenSpaceType.gazelle, GardenSpaceType.elephant},
			{GardenSpaceType.ostrich, GardenSpaceType.locked, GardenSpaceType.locked, GardenSpaceType.locked},
			{GardenSpaceType.locked, GardenSpaceType.locked, GardenSpaceType.locked, GardenSpaceType.locked},
			{GardenSpaceType.locked, GardenSpaceType.locked, GardenSpaceType.locked, GardenSpaceType.locked}};
	// TODO: make it so that everything isn't unlocked always
	
	
	
	public GardenOptionsWindow(Garden garden, GraphicsManager gMan){
		this.garden = garden;
		for (int i = 0; i < GRID_WIDTH; i++){
			for (int j = 0; j < GRID_HEIGHT; j++){
				DrawableImage temp = new DrawableImage(SCREENX + i*CELL_WIDTH + CELL_WIDTH/2, SCREENY + j*CELL_WIDTH + CELL_WIDTH/2, 
						25, garden.GARDEN_SQUARE_WIDTH, garden.GARDEN_SQUARE_WIDTH, null);
				DrawableImage temp2 = new DrawableImage(SCREENX + i*CELL_WIDTH + CELL_WIDTH/2, SCREENY + j*CELL_WIDTH + CELL_WIDTH/2, 
						26, garden.GARDEN_SQUARE_WIDTH, garden.GARDEN_SQUARE_WIDTH, TextureHolder.optionsWindowBackground);
				images[j][i] = temp;
				backImages[j][i] = temp2;
				gMan.addDrawableEntity(temp2);
				gMan.addDrawableEntity(temp);
			}
		}
		selectedImage = new DrawableImage(SCREENX + selectedChoice[0]*CELL_WIDTH + CELL_WIDTH/2, SCREENY + selectedChoice[1]*CELL_WIDTH + CELL_WIDTH/2, 
				24, garden.GARDEN_SQUARE_WIDTH, garden.GARDEN_SQUARE_WIDTH, TextureHolder.tileSelect);
		gMan.addDrawableEntity(selectedImage);
		refreshImages();
	}
	
	// The parameters are relative to the gardenWindow itself
	public void handleClick(int x, int y, WarTug game){
		// I like dried Strawberries.
		int i = x/CELL_WIDTH;
		int j = y/CELL_WIDTH;
		if (i >= 0 && i < GRID_WIDTH && j >= 0 && j < GRID_HEIGHT){
			if (game.gardenUnlocksAvailable > 0 && choices[j][i] == GardenSpaceType.unlock){
				selectedChoice[0] = j;
				selectedChoice[1] = i;
				if (garden.getSelectedSpaceType() == GardenSpaceType.locked){
					garden.setSpace(GardenSpaceType.gazelle);
					refreshImages();
					game.getGardenEntity().buttons.refreshImages();
					game.network.sendGardenToServer(garden);
					game.gardenUnlocksAvailable -= 1;
				}
			} else if (choices[j][i] != GardenSpaceType.locked){
				selectedChoice[0] = j;
				selectedChoice[1] = i;
				if (garden.getSelectedSpaceType() != GardenSpaceType.locked){
					garden.setSpace(choices[selectedChoice[0]][selectedChoice[1]]);
					refreshImages();
					game.getGardenEntity().buttons.refreshImages();
					game.network.sendGardenToServer(garden);
				}
			}
		}
	}
	
	public Garden getGarden(){
		return garden;
	}
	
	public void refreshImages(){
		selectedImage.setX(SCREENX + selectedChoice[1]*CELL_WIDTH + CELL_WIDTH/2);
		selectedImage.setY(SCREENY + selectedChoice[0]*CELL_WIDTH + CELL_WIDTH/2);
		for (int i = 0; i < GRID_WIDTH; i++){
			for (int j = 0; j < GRID_WIDTH; j++){
				switch (choices[j][i]) {
				case lion:
					images[j][i].setTexture(TextureHolder.lion);
					break;
				case gazelle:
					images[j][i].setTexture(TextureHolder.gazelle);
					break;
				case elephant:
					images[j][i].setTexture(TextureHolder.elephant);
					break;
				case ostrich:
					images[j][i].setTexture(TextureHolder.ostrich);
					break;
				default:
					break;
				}	
			}
		}
	}
	
	
	
}
